package com.book.bookshop.web;

import com.book.bookshop.entity.Address;
import com.book.bookshop.entity.BsUser;
import com.book.bookshop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;




    /**
     * todo 当地址选中默认的时候,查询默认的地址,改为0,保持默认地址只有一个
     * @param address
     * @param httpSession
     * @return
     */
    @ResponseBody
    @RequestMapping("/save")
    public String save(Address address, HttpSession httpSession){
        BsUser user = (BsUser) httpSession.getAttribute("user");
        address.setUserId(user.getId());
        if(address.getIsDefault() != null && address.getIsDefault().equals("on")){
            address.setIsDefault("1");
        }
        addressService.save(address);
        return "success";


    }
}
